package com.livegoods.service;

import com.livegoods.pojo.Banner;
import com.livegoods.pojo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Value("${livegoods.fastdfs.nginx}")
    private String nginx;


    @Override
    public LivegoodsResult findBanners() {

      try{
          List<Banner> banners = mongoTemplate.findAll(Banner.class);
          List<String> imgsAddress = new ArrayList<>();
          for (Banner banner : banners) {
              imgsAddress.add(nginx+banner.getPath());
          }
          LivegoodsResult livegoodsResult = LivegoodsResult.ok();
          livegoodsResult.setResults(imgsAddress);
          return livegoodsResult;
      }catch (Exception e){
          return LivegoodsResult.error("查询Banner失败，请联系管理员..");
      }

    }
}
